/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.dialogs;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import org.eclipse.swt.widgets.Display;
import org.mags.common.EngineContext;
import org.mags.object.common.ParserFormatterUtils;
import org.mags.remas.model.entity.Entity;
import org.mags.remas.model.entity.EntityComment;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.model.entity.EnumValue;
import org.mags.remas.model.entity.KeyDefinition;
import org.mags.remas.model.entity.KeyValue;
import org.mags.remas.model.entity.provider.KeyTypeProvider;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class FindRelationshipsDialog extends AbstractFindRelationshipsDialog {
	private EntityRelationship filter;
	public EntityRelationship getFilter() {
		return filter;
	}
	public void setFilter(EntityRelationship filter) {
		this.filter = filter;
	}
	public FindRelationshipsDialog(Display display) {
		super(display);
		super.setText("find entites");
	}
	@Override
	public void accept() {
		Entity entity = new Entity();
		entity.setCode(super.getCode());
		entity.setName(super.getEntityName());
		entity.setDescription(super.getDescription());
		entity.setEntityType(super.getEntityType());
		EntityComment entityComment  = new EntityComment();
		entityComment.setComment(super.getComment());
		entityComment.setCommentType(super.getCommentType());
		entity.getEntityComments().add(entityComment);
		Map<KeyDefinition, Object> keyValues = super.getKeys();
		entity.getKeyValues().clear();
		KeyValue keyValue;
		KeyTypeProvider keyTypeProvider = EngineContext.getDataContext().getEntityProviderFactory().getKeyTypeProvider();
		for(KeyDefinition i: keyValues.keySet()) {
			keyValue = new KeyValue();
			keyValue.setKeyDefinition(i);
			
			if(keyTypeProvider.isDATE(i.getKeyType()))
					keyValue.setDateValue((Date)keyValues.get(i));
			else if(keyTypeProvider.isINTEGER(i.getKeyType())) {
				String strValue = (String)keyValues.get(i);
				if(!strValue.equals(""))
					keyValue.setNumericValue(new BigDecimal(ParserFormatterUtils.getInstance().parseInteger(strValue)));
			}
			else if(keyTypeProvider.isFLOAT(i.getKeyType())) {
				String strValue = (String)keyValues.get(i);
				if(!strValue.equals(""))
					keyValue.setNumericValue(new BigDecimal(ParserFormatterUtils.getInstance().parseInteger(strValue)));
			}					
			else if(keyTypeProvider.isVARCHAR(i.getKeyType())) 
				keyValue.setVarcharValue((String)keyValues.get(i));
			else if(keyTypeProvider.isENUM(i.getKeyType()))
				keyValue.setEnumValue((EnumValue)keyValues.get(i));
			else if(keyTypeProvider.isLIST(i.getKeyType()))
				keyValue.setVarcharValue((String)keyValues.get(i));
			entity.getKeyValues().add(keyValue);
		}
		filter = new EntityRelationship();
		filter.setEntity(entity);
		super.hide();
	}
	@Override
	public void cancel() {
		filter = null;
		super.hide();
	}
}
